﻿Public Class WebForm4
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then Return
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MyProjectConnection").ConnectionString)
        Dim SqlString As String = "Insert into itemsviewed(Itemfk, loggedinuser) Values (@f1,@f2)"
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(SqlString, oleDbConn)
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

    End Sub

    Protected Sub BT_Comments_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BT_Comments.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MyProjectConnection").ConnectionString)
        Dim SqlString As String = "Insert into Comment(Comment, Itemfk, author) Values (@f1,@f2,@f3)"
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", TB_Comment.Text)
        cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("confirmation.aspx")
    End Sub

    Protected Sub BT_addtag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BT_AddTag.Click
        Dim ItemId = Request.Params("ID")
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("connectionstring").ConnectionString)
        conn.Open()
        Dim newTags = TB_Addtag.Text.Split(",")

        For Each newTag As String In newTags
            Dim isTagThereCmd As New OleDb.OleDbCommand("SELECT * FROM Tag WHERE Tag=@f1", conn)
            isTagThereCmd.Parameters.AddWithValue("@f1", newTag)
            Dim dr = isTagThereCmd.ExecuteReader()
            If Not dr.HasRows Then
                Dim addTagCmd As New OleDb.OleDbCommand("INSERT INTO Tag (Tag) VALUES (@f1)", conn)
                addTagCmd.Parameters.AddWithValue("@f1", newTag)
                addTagCmd.ExecuteNonQuery()
            End If
            Dim findNewTagIDCmd As New OleDb.OleDbCommand("SELECT ID FROM Tag WHERE Tag=@f1", conn)
            findNewTagIDCmd.Parameters.AddWithValue("@f1", newTag)
            Dim newTagDr = findNewTagIDCmd.ExecuteReader()
            newTagDr.Read()
            Dim newID = newTagDr.Item("ID")
            Dim makeAssociationCmd As New OleDb.OleDbCommand("INSERT INTO itemtag (tagfk,itemfk) VALUES (@f1,@f2)", conn)
            makeAssociationCmd.Parameters.AddWithValue("@f1", newID)
            makeAssociationCmd.Parameters.AddWithValue("@f2", ItemId)
            makeAssociationCmd.ExecuteNonQuery()
            Response.Redirect("confirmation.aspx")

        Next


        For Each li As ListItem In CheckBoxList1.Items
            If (li.Selected = True) Then
                Dim sql = "INSERT INTO itemtag (tagfk,itemfk) VALUES (@f1,@f2)"
                Dim cmd As New OleDb.OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", li.Value)
                cmd.Parameters.AddWithValue("@f2", ItemId)
                cmd.ExecuteNonQuery()
                Response.Redirect("confirmation.aspx")
            End If
        Next
        conn.Close()
    End Sub

    Protected Sub TB_Comment_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TB_Comment.TextChanged

    End Sub

    Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBoxList1.SelectedIndexChanged

    End Sub

    Protected Sub TB_Addtag_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TB_Addtag.TextChanged

    End Sub
End Class